Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Expect.js is a minimalistic assertion library for Node.js and the browser. It provides a simple and expressive syntax for writing tests, allowing developers to assert conditions in their code.
Assertions
Expect.js allows you to assert that a value is of a certain type. In this example, it checks if the value 5 is a number.
expect(5).to.be.a('number');
Equality
You can assert that two values are equal using expect.js. This example checks if the number 5 is equal to 5.
expect(5).to.equal(5);
Truthiness
Expect.js can be used to assert the truthiness of a value. Here, it checks if the value true is truthy.
expect(true).to.be.ok();
Existence
This feature allows you to assert whether a value exists or not. In this example, it checks that the value undefined does not exist.
expect(undefined).to.not.exist();
Length
Expect.js can assert the length of arrays or strings. This example checks if the array [1, 2, 3] has a length of 3.
expect([1, 2, 3]).to.have.length(3);
Chai is a popular assertion library for Node.js and the browser that offers a more extensive API than expect.js. It supports BDD and TDD styles and provides more flexibility with plugins and additional features.
Jest is a comprehensive testing framework that includes a built-in assertion library. It offers more features than expect.js, such as mocking and snapshot testing, making it suitable for larger projects.
Should.js is another assertion library that provides a similar expressive syntax to expect.js. It offers more extensive chaining and additional features, making it a more powerful alternative for complex assertions.
Minimalistic BDD assertion toolkit based on should.js
expect(window.r).to.be(undefined);
expect({ a: 'b' }).to.eql({ a: 'b' })
expect(5).to.be.a('number');
expect([]).to.be.an('array');
expect(window).not.to.be.an(Image);
require('expect.js')
).Install it with NPM or add it to your package.json
:
$ npm install expect.js
Then:
var expect = require('expect.js');
Expose the expect.js
found at the top level of this repository.
<script src="expect.js"></script>
ok: asserts that the value is truthy or not
expect(1).to.be.ok();
expect(true).to.be.ok();
expect({}).to.be.ok();
expect(0).to.not.be.ok();
be / equal: asserts ===
equality
expect(1).to.be(1)
expect(NaN).not.to.equal(NaN);
expect(1).not.to.be(true)
expect('1').to.not.be(1);
eql: asserts loose equality that works with objects
expect({ a: 'b' }).to.eql({ a: 'b' });
expect(1).to.eql('1');
a/an: asserts typeof
with support for array
type and instanceof
// typeof with optional `array`
expect(5).to.be.a('number');
expect([]).to.be.an('array'); // works
expect([]).to.be.an('object'); // works too, since it uses `typeof`
// constructors
expect(5).to.be.a(Number);
expect([]).to.be.an(Array);
expect(tobi).to.be.a(Ferret);
expect(person).to.be.a(Mammal);
match: asserts String
regular expression match
expect(program.version).to.match(/[0-9]+\.[0-9]+\.[0-9]+/);
contain: asserts indexOf for an array or string
expect([1, 2]).to.contain(1);
expect('hello world').to.contain('world');
length: asserts array .length
expect([]).to.have.length(0);
expect([1,2,3]).to.have.length(3);
empty: asserts that an array is empty or not
expect([]).to.be.empty();
expect({}).to.be.empty();
expect({ length: 0, duck: 'typing' }).to.be.empty();
expect({ my: 'object' }).to.not.be.empty();
expect([1,2,3]).to.not.be.empty();
property: asserts presence of an own property (and value optionally)
expect(window).to.have.property('expect')
expect(window).to.have.property('expect', expect)
expect({a: 'b'}).to.have.property('a');
key/keys: asserts the presence of a key. Supports the only
modifier
expect({ a: 'b' }).to.have.key('a');
expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c');
expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']);
expect({ a: 'b', c: 'd' }).to.not.only.have.key('a');
throwException/throwError: asserts that the Function
throws or not when called
expect(fn).to.throwError(); // synonym of throwException
expect(fn).to.throwException(function (e) { // get the exception object
expect(e).to.be.a(SyntaxError);
});
expect(fn).to.throwException(/matches the exception message/);
expect(fn2).to.not.throwException();
withArgs: creates anonymous function to call fn with arguments
expect(fn).withArgs(invalid, arg).to.throwException();
expect(fn).withArgs(valid, arg).to.not.throwException();
within: asserts a number within a range
expect(1).to.be.within(0, Infinity);
greaterThan/above: asserts >
expect(3).to.be.above(0);
expect(5).to.be.greaterThan(3);
lessThan/below: asserts <
expect(0).to.be.below(3);
expect(1).to.be.lessThan(3);
fail: explicitly forces failure.
expect().fail()
expect().fail("Custom failure message")
For example, if you create a test suite with mocha.
Let's say we wanted to test the following program:
math.js
function add (a, b) { return a + b; };
Our test file would look like this:
describe('test suite', function () {
it('should expose a function', function () {
expect(add).to.be.a('function');
});
it('should do math', function () {
expect(add(1, 3)).to.equal(4);
});
});
If a certain expectation fails, an exception will be raised which gets captured and shown/processed by the test runner.
should
methods like should.strictEqual
. For example,
expect(obj).to.be(undefined)
works well.Clone the repository and install the developer dependencies:
git clone git://github.com/LearnBoost/expect.js.git expect
cd expect && npm install
make test
make test-browser
and point your browser(s) to http://localhost:3000/test/
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <guillermo@learnboost.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Heavily borrows from should.js by TJ Holowaychuck - MIT.
FAQs
BDD style assertions for node and the browser.
We found that expect.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.